package com.itheima.pinda.web.user;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.pinda.DTO.user.TruckDriverDto;
import com.itheima.pinda.DTO.user.TruckDriverLicenseDto;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.user.PdTruckDriver;
import com.itheima.pinda.entity.user.PdTruckDriverLicense;
import com.itheima.pinda.mapstruct.TruckDriverDTO2Entity;
import com.itheima.pinda.service.PdTruckDriverLicenseService;
import com.itheima.pinda.service.PdTruckDriverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 司机相关
 *
 * @author llhuang10
 * @date 2021/10/1 19:24
 */
@Slf4j
@Validated
@Api(tags = "司机管理")
@RestController
@RequestMapping("/sys/driver")
public class DriverController {


    @Resource
    private PdTruckDriverService pdTruckDriverService;

    @Resource
    private PdTruckDriverLicenseService pdTruckDriverLicenseService;

    @Resource
    private TruckDriverDTO2Entity driverDTO2Entity;

    /**
     * 保存 | 更新 司机基本信息   取决于userId是否为空
     *
     * @param dto 司机基本信息
     * @return 返回信息
     */
    @PostMapping
    @ApiOperation("保存司机基本信息")
    public TruckDriverDto saveDriver(@Valid @RequestBody TruckDriverDto dto) {
        return pdTruckDriverService.saveTruckDriver(dto);
    }

    /**
     * 获取司机基本信息列表
     *
     * @param userIds 司机id列表
     * @param fleetId 司机所属车队Id
     * @return 司机基本信息列表
     */
    @GetMapping
    @ApiOperation("获取司机基本信息列表")
    public List<TruckDriverDto> findAllDriver(@RequestParam(name = "userIds", required = false) List<String> userIds,
                                              @RequestParam(name = "fleetId", required = false) String fleetId) {
        return pdTruckDriverService.findAllDriver(userIds, fleetId);
    }

    /**
     * 根据司机userId获取司机基本信息
     *
     * @param userId 司机userId
     * @return 司机基本信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据司机userId获取司机基本信息")
    public TruckDriverDto findOneDriver(@NotBlank(message = "司机id不能为空") @PathVariable("id") String userId) {
        // todo 注意点: @PathVariable注解, 若方法参数名和需要绑定的url中变量名称不一致时, 必须特别指出属性名, 如@PathVariable("id")
        PdTruckDriver pdTruckDriver = pdTruckDriverService.getOne(Wrappers.<PdTruckDriver>lambdaQuery()
                .eq(PdTruckDriver::getUserId, userId));
        return driverDTO2Entity.conver(pdTruckDriver);
    }

    /**
     * 保存司机驾驶证信息
     *
     * @param dto 司机驾驶证信息
     * @return TruckDriverLicenseDto
     */
    @PostMapping("/driverLicense")
    @ApiOperation("保存司机驾驶证信息")
    public TruckDriverLicenseDto saveDriverLicense(@Valid @RequestBody TruckDriverLicenseDto dto) {
        return pdTruckDriverLicenseService.saveDriverLicense(dto);
    }

    /**
     * 根据司机userId获取司机驾驶证信息
     *
     * @param id 司机id
     * @return 司机驾驶证信息
     */
    @GetMapping("/{id}/driverLicense")
    @ApiOperation("根据司机userId获取司机驾驶证信息")
    public TruckDriverLicenseDto findOneDriverLicense(@NotBlank(message = "司机id不能为空") @PathVariable("id") String id) {
        PdTruckDriverLicense pdTruckDriver = pdTruckDriverLicenseService.getOne(Wrappers.<PdTruckDriverLicense>lambdaQuery()
                .eq(PdTruckDriverLicense::getUserId, id));
        return driverDTO2Entity.conver(pdTruckDriver);
    }

    /**
     * 统计司机数量
     *
     * @param fleetId 车队id
     * @return 司机数量
     */
    @GetMapping("/count")
    @ApiOperation("统计司机数量")
    public Integer count(@RequestParam(name = "fleetId", required = false) String fleetId) {
        return pdTruckDriverService.count(Wrappers.<PdTruckDriver>lambdaQuery()
                .eq(StringUtils.isNotBlank(fleetId), PdTruckDriver::getFleetId, fleetId));
    }


    /**
     * 获取司机分页数据
     *
     * @param page     页码
     * @param pageSize 页尺寸
     * @param fleetId  车队id
     * @return 司机分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取司机分页数据")
    public PageResponse<TruckDriverDto> findByPage(@RequestParam(name = "page") Integer page,
                                                   @RequestParam(name = "pageSize") Integer pageSize,
                                                   @RequestParam(name = "fleetId", required = false) String fleetId) {
        return pdTruckDriverService.findByPage(page, pageSize, fleetId);
    }


    /**
     * 根据司机userId集合列表查询
     *
     * @param ids 司机userId集合
     * @return 司机基本信息集合
     */
    @GetMapping("/findAll")
    @ApiOperation("根据司机userId集合列表查询")
    public List<TruckDriverDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids) {
        List<PdTruckDriver> list = pdTruckDriverService.list(Wrappers.<PdTruckDriver>lambdaQuery()
                .in(ids != null && ids.size() > 0, PdTruckDriver::getUserId, ids));

        return list.stream()
                .map(item -> driverDTO2Entity.conver(item))
                .collect(Collectors.toList());
    }
}
